﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ctrip.Automation.Framework.Lib;
using System.Collections;
using System.Net;
using Newtonsoft.Json;
using System.Xml.Serialization;
using System.Threading;
using System.Diagnostics;

namespace API.Taolx.Shopping.Common
{

    public class SoaAPI
    {

        /// <summary>
        /// 获取OpenAPI JSON返回体
        /// </summary>
        public void GetJsonResponse(Dictionary<string, string> bodyParas, Dictionary<string, string> headParas,string  url)
        {
            string request = File.ReadAllText(string.Format("RequestJson\\ShoppingAPI\\Api-Product-ProductPriceDate\\Api-Product-ProductPriceDate.json"));
            string JsonResponse = String.Empty;
            if (headParas != null)
            {
                JObject requestObj = JObject.Parse(request);
                foreach (KeyValuePair<string, string> hp in headParas)
                {
                    requestObj[hp.Key] = hp.Value;
                }
                request = requestObj.ToString();
            }
            Log.Info("请求体：" + request);
            JsonResponse = postAPI2(url, request);
            CtripAssert.IsNotNull(JsonResponse);
        }

        #region 接口调用

        public static string postAPI2(string uri, string requestjson)
        {
            if (string.IsNullOrEmpty(uri))
                uri = "http://shoppingapi.test2.taolx.com/api/Product/ProductPriceDate";

            string hostname = string.Empty;
            Uri uriAddress = new Uri(uri);
            /*  if (!string.IsNullOrEmpty(hostip))
              {
                  hostname = uriAddress.Host;
                  uriAddress = new Uri("http://" + hostip + uriAddress.AbsolutePath);
              }
             */

            HttpWebRequest request = WebRequest.Create(uriAddress) as HttpWebRequest;
            request.ContentType = "application/json;charset=\"utf-8\"";
            request.Accept = "application/json";
            request.Method = "POST";
            if (!string.IsNullOrEmpty(hostname))
                request.Host = hostname;
            Trace.WriteLine(uri);
            Trace.WriteLine(requestjson);
            StringBuilder postData = new StringBuilder(requestjson);

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] data = encoding.GetBytes(postData.ToString());
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            string responsejson = string.Empty;
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    responsejson = sr.ReadToEnd();
                }
            }
            Trace.WriteLine(responsejson);
            return responsejson;
        }



        #endregion

    }
}
